package com.edu.aspect;
/*
 * @Author:princess
 * @Date 2023/10/31 10 31 20:58
 *
 */

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
@Aspect
@Component
public class LogAspect {

    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* com.edu.service..*(..))")
    public void pt1(){}

    @After("pt1()")
    public void before(JoinPoint jt){
        System.out.println("执行了前置通知");
    }

    @AfterReturning(value = "pt1()",returning = "result")
    public void afterRetrun(JoinPoint jt,Object result){
        System.out.println("执行了后置返回通知");
    }

    @AfterThrowing(value = "pt1()",throwing = "throwable")
    public void afterThrowing(JoinPoint jt,Throwable throwable){
        System.out.println("执行了后置异常通知");
    }

    @After("pt1()")
    public void after(JoinPoint jt){
        System.out.println("执行了后置通知");
    }

    @Around("pt1()")
    public Object around(ProceedingJoinPoint jt) throws Throwable {
        System.out.println("执行了环绕的前置通知");
        long start = System.currentTimeMillis();
        Object result = jt.proceed();
        long end = System.currentTimeMillis();
        logger.debug("时间:"+new Date()+"\t"+jt.getTarget()+"\t"+"方法名："+jt.getSignature().getName()+"\t"+"耗时："+(end-start));
        System.out.println("执行了环绕的后置通知");
        return result;
    }
}
